﻿using System;
using System.Windows;
using LayeredArchitectureSample.Infrastructure.Services;
using LayeredArchitectureSample.RandomDataProviders;
using LayeredArchitectureSample.ServiceContracts;
using LayeredArchitectureSample.ViewModel;

namespace LayeredArchitectureSample.ClientBrowser
{
    internal static class EntryPoint
    {
        [STAThread]
        static void Main()
        {
            var application = new Application();

            var wareRepository = new ServiceOperationInvoker<IRemoteWareRepository>(
                ServiceChannelFactory.CreateDefault<IRemoteWareRepository>(
                    "net.tcp://localhost:8732/Design_Time_Addresses/RemoteWareRepository/",
                    BindingFactory.CreateDefaultBinding()));

            var locationRepository = new ServiceOperationInvoker<IRemoteLocationRepository>(
                ServiceChannelFactory.CreateDefault<IRemoteLocationRepository>(
                "net.tcp://localhost:8732/Design_Time_Addresses/RemoteLocationRepository/",
                BindingFactory.CreateDefaultBinding()));

            var routeRepository = new ServiceOperationInvoker<IRemoteRouteRepository>(
                ServiceChannelFactory.CreateDefault<IRemoteRouteRepository>(
                "net.tcp://localhost:8732/Design_Time_Addresses/RemoteRouteRepository/",
                BindingFactory.CreateDefaultBinding()));

            var tripManager = new ServiceOperationInvoker<ITripManager>(
                ServiceChannelFactory.CreateDefault<ITripManager>(
                "net.tcp://localhost:8732/Design_Time_Addresses/TripManager/",
                BindingFactory.CreateDefaultBinding()));

            IRandomRouteProvider randomRouteProvider = new RandomRouteProvider(routeRepository);
            IRandomWareProvider randomWareProvider = new RandomWareProvider(wareRepository);
            IRandomLocationProvider randomLocationProvider = new RandomLocationProvider(locationRepository);

            var window = new MainView(
                new MainViewModel(
                    new ApplicationController(),
                    wareRepository,
                    locationRepository,
                    routeRepository,
                    tripManager,
                    randomRouteProvider,
                    randomWareProvider,
                    randomLocationProvider,
                    new ExceptionAwareServiceCodeRunner(new UserInteractionService())
                    ));

            application.Run(window);
        }
    }
}